<template>
  <n-card :bordered="false" class="proCard">
    <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset">
      <template #statusSlot="{ model, field }">
        <n-input v-model:value="model[field]" />
      </template>
    </BasicForm>

    <BasicTable
      :columns="columns"
      :request="loadDataTable"
      :row-key="(row) => row.id"
      ref="actionRef"
      :actionColumn="actionColumn"
      @update:checked-row-keys="onCheckedRow"
      :row-class-name="rowClassName"
      :scroll-x="1090"
    >
      <template #tableTitle> </template>

      <template #toolbar>
        <n-button type="primary" @click="reloadTable">刷新数据</n-button>
      </template>
    </BasicTable>
  </n-card>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { useDialog, useMessage } from 'naive-ui';
  import { BasicTable, TableAction } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import {
    getMemberWithdrawalPage,
    passMemberWithdrawal,
    payMemberWithdrawal,
    refuseMemberWithdrawal,
  } from '@/api/member/memberWithdrawal';
  import { columns } from './columns';
  import { ResultEnum } from '@/enums/httpEnum';

  const schemas: FormSchema[] = [
    {
      field: 'mobile',
      component: 'NInput',
      label: '手机号',
      rules: [{ required: false }],
    },
    {
      field: 'status',
      component: 'NSelect',
      label: '状态',
      componentProps: {
        placeholder: '请选择状态',
        options: [
          {
            label: '待审核',
            value: 0,
          },
          {
            label: '审核通过',
            value: 1,
          },
          {
            label: '驳回',
            value: 2,
          },
          {
            label: '打款成功',
            value: 3,
          },
          {
            label: '关闭',
            value: 4,
          },
        ],
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
      rules: [{ required: false }],
    },
    // {
    //   field: 'createTime',
    //   component: 'NInput',
    //   label: '创建时间',
    //   rules: [{ required: false }],
    // },
    // {
    //   field: 'updateTime',
    //   component: 'NInput',
    //   label: '更新时间',
    //   rules: [{ required: false }],
    // },
  ];

  const actionRef = ref();
  const params = ref({});
  const message = useMessage();
  const dialog = useDialog();
  const actionColumn = reactive({
    width: 220,
    title: '操作',
    key: 'action',
    fixed: 'right',
    render(record) {
      let actions: Array<{}> = [];

      if (record.status == 0) {
        actions.push({
          label: '通过审核',
          onClick: handlePass.bind(null, record),
        });

        actions.push({
          label: '驳回',
          icon: 'ic:outline-delete-outline',
          onClick: handleRefuse.bind(null, record),
        });
      }

      if (record.status == 1) {
        actions.push({
          label: '确认打款',
          onClick: handlePay.bind(null, record),
        });
      }

      return h(TableAction as any, {
        style: 'button',
        actions: actions,
      });
    },
  });

  const [register, {}] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 100,
    schemas,
  });

  const loadDataTable = async (res) => {
    return await getMemberWithdrawalPage({ ...params.value, ...res });
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  function rowClassName(row) {
    if (row.status === 0) {
      return 'withdrawal-review';
    }
    if (row.status === 1) {
      return 'withdrawal-pass';
    }
    if (row.status === 2) {
      return 'withdrawal-refuse';
    }
    if (row.status === 3) {
      return 'withdrawal-pay';
    }
    if (row.status === 4) {
      return 'withdrawal-close';
    }
    return '';
  }

  function handlePass(record: Recordable) {
    dialog.info({
      title: '提示',
      content: `您确定想通过审核吗?`,
      positiveText: '确定',
      negativeText: '取消',
      onPositiveClick: async () => {
        const params = { id: record.id };
        const response = await passMemberWithdrawal(params);
        const { message: msg, code } = response;
        if (code == ResultEnum.SUCCESS) {
          message.success(msg);
          reloadTable();
        } else {
          message.error(msg);
        }
      },
      onNegativeClick: () => {
        message.error('已取消');
      },
    });
  }

  /**
   * 审核拒绝
   */
  function handleRefuse(record: Recordable) {
    dialog.info({
      title: '提示',
      content: `您确定驳回吗?`,
      positiveText: '确定',
      negativeText: '取消',
      onPositiveClick: async () => {
        const params = { id: record.id };
        const response = await refuseMemberWithdrawal(params);
        const { message: msg, code } = response;
        if (code == ResultEnum.SUCCESS) {
          message.success(msg);
          reloadTable();
        } else {
          message.error(msg);
        }
      },
      onNegativeClick: () => {
        message.error('已取消');
      },
    });
  }

  function handlePay(record: Recordable) {
    dialog.info({
      title: '提示',
      content: `您确定想确认打款吗?`,
      positiveText: '确定',
      negativeText: '取消',
      onPositiveClick: async () => {
        const params = { id: record.id };
        const response = await payMemberWithdrawal(params);
        const { message: msg, code } = response;
        if (code == ResultEnum.SUCCESS) {
          message.success(msg);
          reloadTable();
        } else {
          message.error(msg);
        }
      },
      onNegativeClick: () => {
        message.error('已取消');
      },
    });
  }

  function handleSubmit(values: Recordable) {
    console.log(values);
    params.value = values;
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
  }
</script>

<style lang="less" scoped>
  :deep(.withdrawal-review .status) {
    color: #00ABE9 !important;
  }
  :deep(.withdrawal-pass .status) {
    color: #20a53a !important;
  }
  :deep(.withdrawal-pay .status) {
    color: #0f0 !important;
  }
  :deep(.withdrawal-close .status) {
    color: #f00 !important;
  }
  :deep(.withdrawal-refuse .status) {
    color: #f00 !important;
  }
</style>
